$(function() {
    is_login();

    $('#post_btn').click(function() {
        if (check_post()) {
            is_login(function() {
                post_editor.sync();
                $('#post_form').submit();
            });
        }
    });
    $('#post_preview').click(function() {
        if (check_post()) {
            $('#post_preview_title').html($('#post_title').val());
            var content =
                '<div class="well">'
                    + ($('#post_male').hasClass('active') ? '男，' : '女，')
                    + $('#post_constel').val() + '，'
                    + $('#post_birth').val() + '年生人，现居'
                    + $('#post_residence').val() + '。' +
                '</div>' + post_editor.getContent();
            $('#post_preview_content').html(content);

            $('#post_preview_modal')
                .css('width', '800px')
                .css('margin-left', '-400px')
                .css('margin-top', '-50px')
                .modal('show');
        }
    });
    $('#post_birth').focus(function() {
        if ($(this).hasClass('post_error')) {
            $(this).select();
        }
    });
    $('#post_constel')
        .focus(function() {
            $(this).select();
        })
        .mouseup(function(e) {
            e.preventDefault();
        })
        .popover({
            trigger: 'focus',
            placement: 'bottom',
            html: 'true',
            template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content constel_div"><p></p></div></div></div>'
        });
    // 'click' fail in IE, use 'mousedown' instead
    $(document).on('mousedown', 'td.constel', function() {
        $('#post_constel').val($(this).text());
    });
    $('#post_residence').focus(function() {
        if ($(this).hasClass('post_error')) {
            $(this).select();
        }
    });
    $('#post_error_close').click(function() {
        $('#post_error_div').fadeOut('fast', function() {
            alert_clean();
        });
    });
    $('#post_form').submit(function() {
        $('#post_topic_submit').val($('#post_topic').data('value'));
        $('#post_gender_submit').val($('#post_male').hasClass('active') ? 1 : 0);
        $('#post_constel_submit').val(constel2value($('#post_constel').val()));
        return true;
    });

    $('.post_topic').click(function() {
        $('.post_topic_div').animate({width:'toggle'}, 'slow');
        /* add margin-left to fix style */
        var html = '<a class="btn" id="post_topic" title="点击展开主题" style="margin-left:-5px;display:none;">' + $(this).text() + '</a>';
        $('.post_title_div').prepend(html).addClass('input-prepend');
        $('#post_title').animate({width:'532px'}, 'slow', function() {
            $('#post_topic').animate({width:'toggle'}, 'fast');
        });
        $('#post_topic')
            .data('value', $(this).data('value'))
            .addClass('btn-success');
    });
    /* expand topic choice radio */
    $(document).on('click', '#post_topic', function() {
        $('#post_topic').animate({width:'toggle'}, 'fast', function() {
            $('.post_title_div').removeClass('input-prepend');
            $(this).remove();
            $('#post_title').animate({width:'420px'}, 'slow');
            $('.post_topic_div').animate({width:'toggle'}, 'slow');
        });
    });
});

function check_post() {
	alert_hide();
	
    var pass_check = true;
    var $post_topic_div = $('.post_topic_div a');
    var $post_title = $('#post_title');
    var $post_male = $('#post_male');
    var $post_female = $('#post_female');
    var $post_birth = $('#post_birth');
    var $post_constel = $('#post_constel');
    var $post_residence = $('#post_residence');

    if (($('#post_topic')[0] == undefined)
        && ($('.post_topic_div .active')[0] == undefined)) {
        $post_topic_div.addClass('post_error');
        alert_add('请选择帖子的主题！');
        pass_check = false;
    } else {
        $post_topic_div.removeClass('post_error');
    }
    if ($post_title.val() === '') {
        $post_title.addClass('post_error');
        if ($.browser.msie && ($.browser.version == '7.0')) {
            $post_title.focus(function() {
                if ($(this).hasClass('post_error')) {
                    $(this).val('');
                }
            });
            $post_title.val('标题不能为空！');
        } else {
            $post_title.attr('placeholder', '标题不能为空！').focus();
        }
        alert_add('请输入帖子的标题！');
        pass_check = false;
    } else {
        $post_title.removeClass('post_error');
    }
    if (!$post_male.hasClass('active') && !$post_female.hasClass('active')) {
        $post_male.addClass('post_error');
        $post_female.addClass('post_error');
        alert_add('请选择挂牌对象的性别！');
        pass_check = false;
    } else {
        $post_male.removeClass('post_error');
        $post_female.removeClass('post_error');
    }
    if ((birth_error = check_birth()) != '') {
        $post_birth.addClass('post_error');
        if ($.browser.msie && ($.browser.version == '7.0')) {
            $post_birth.focus(function() {
                if ($(this).hasClass('post_error')) {
                    $(this).val('');
                }
            });
            $post_birth.val(birth_error);
        } else {
            $post_birth.attr('placeholder', birth_error);
        }
        alert_add(birth_error);
        pass_check = false;
    } else {
        $post_birth.removeClass('post_error');
    }
    if ((constel_error = check_constel()) != '') {
        $post_constel.addClass('post_error');
        alert_add(constel_error);
        pass_check = false;
    } else {
        $post_constel.removeClass('post_error');
    }
    if ($post_residence.val() === '') {
        $post_residence.addClass('post_error');
        if ($.browser.msie && ($.browser.version == '7.0')) {
            $post_residence.focus(function() {
                if ($(this).hasClass('post_error')) {
                    $(this).val('');
                }
            });
            $post_residence.val('请输入现居地点！');
        } else {
            $post_residence.attr('placeholder', '请输入现居地点！');
        }
        alert_add('请输入现居地点！');
        pass_check = false;
    } else {
        $post_residence.removeClass('post_error');
    }
    if (!pass_check) {
        $('#post_error_div').fadeIn('fast');
        return false;
    } else {
        return true;
    }
}

function check_birth() {
    var error0 = '请输入出生年份！';
    var error1 = '请输入正确的出生年份！';
    var $birth = $('#post_birth');
    var birth = parseInt($birth.val());
    if ($birth.val() == '') {
        return error0;
    } else if (birth != NaN) {
        var current = new Date();
        var num_length = birth.toString().length;
        if (num_length == 2) {
            if (birth < (current.getYear() - 100)) {
                $birth.val(birth + 2000);
            } else {
                $birth.val(birth + 1900);
            }
        } else if (num_length == 4) {
            if ((birth > current.getFullYear())
                || (current.getFullYear() - birth > 200)) {
                return error1;
            }
        } else {
            return error1;
        }
    } else {
        return error1;
    }
    return '';
}

function check_constel() {
    var constel = $('#post_constel').val();
    if (constel === '') {
        return '请选择星座！';
    } else if (constel2value(constel) === -1) {
        return '请输入正确的星座！';
    } else {
        return '';
    }
}

function constel2value(constel) {
    switch (constel) {
        case '摩羯座':
            return 1;
        case '水瓶座':
            return 2;
        case '双鱼座':
            return 3;
        case '白羊座':
            return 4;
        case '金牛座':
            return 5;
        case '双子座':
            return 6;
        case '巨蟹座':
            return 7;
        case '狮子座':
            return 8;
        case '处女座':
            return 9;
        case '天秤座':
            return 10;
        case '天蝎座':
            return 11;
        case '射手座':
            return 12;
        default:
            return -1;
    }
}

function alert_add(message) {
    $('#post_error ul').append('<li>' + message + '</li>');
}

function alert_clean() {
    $('#post_error ul').remove();
    $('#post_error').append('<ul></ul>');
}

function alert_hide() {
	var $error_div = $('#post_error_div');
	if ($error_div.css('display') == 'block') {
        alert_clean();
		$error_div.hide();
	}
}